package com.example.administrator.myapplication.util;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2020/12/17.
 */

public class GetJsonDateUtil {
    //读取本地Json文件步骤
    //第一步：创建字符串缓存区
    public String getJson(Context context,String fileName){
        StringBuilder stringBuilder =new StringBuilder();
        //第二步：根据本地资源管理器获取到assets目录下的数据信息
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String str ;
            while ((str = bufferedReader.readLine())!=null){
                stringBuilder.append(str);//向字符串缓存区添加数据
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();//将缓冲区数据返回
    }
}
